package cs235.spellcheck;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpellCheck implements SpellingChecker {
	private Set<String> dictionary;
	private Map<String, List<Integer>> misspelled;

	private final static String wordSearch = "\\b[a-zA-Z_0-9]*\\b";

	public SpellCheck() {
		dictionary = new HashSet<String>();
	}

	@Override
	public boolean loadDictionary(String fileName) {
		try {
			BufferedReader br = openFile(fileName);
			String line;
			do {
				line = br.readLine();
				dictionary.add(line);
			} while (line != null);
			br.close();
			return true;

		} catch (FileNotFoundException e) {
			System.out.println("The file you provided was not found");
		} catch (IOException e) {
		} catch (NullPointerException e) {
		}
		return false;
	}
	
	private BufferedReader openFile(String fileName){
		if (fileName == null) {
			throw new IllegalArgumentException("Filename cannot be null");
		}
		InputStreamReader is = new InputStreamReader(this.getClass()
				.getResourceAsStream(fileName));
		return new BufferedReader(is);
	}

	@Override
	public String checkDocument(String fileName) {
		
		misspelled = new TreeMap<String, List<Integer>>();
		try {
			BufferedReader br = openFile(fileName);
			String line;
			Integer lineNumber = 0;
			do {
				line = br.readLine();
				processLine(line, ++lineNumber);
			} while (line != null);

			br.close();
			return mapToString();

		} catch (FileNotFoundException e) {
			System.out.println("The file you provided was not found");
		} catch (IOException e) {
		} catch (NullPointerException e) {
		}
		return null;
	}

	private int processLine(String line, Integer lineNumber) {
		if (line == null) {
			return 0;
		}
		Pattern word = Pattern.compile(wordSearch);
		Matcher findWords = word.matcher(line);
		String temp;
		while (findWords.find()) {
			temp = line.substring(findWords.start(), findWords.end())
					.toLowerCase().replaceAll("[0-9]", "");
			if (!dictionary.contains(temp)) {
				if (!misspelled.containsKey(temp)) {
					misspelled.put(temp, new ArrayList<Integer>());
				}
				((ArrayList<Integer>) misspelled.get(temp)).add(lineNumber);
			}
		}
		return 1;
	}

	private String mapToString() {
		StringWriter aStringAString = new StringWriter();
		PrintWriter outWrite = new PrintWriter(aStringAString);
		Iterator<String> itr = misspelled.keySet().iterator();
		while (itr.hasNext()) {
			String current = (String) itr.next();
			if (current.matches("\\s*") || current.matches("\\d*")) {
				continue;
			}
			outWrite.print(current + ": ");
			List<Integer> list = misspelled.get(current);
			Iterator<Integer> itr2 = list.iterator();
			Integer count = list.size();
			while (itr2.hasNext()) {
				outWrite.print(itr2.next().toString());
				if (--count != 0) {
					outWrite.print(", ");
				}
			}
			outWrite.println("");
		}

		outWrite.flush();
		outWrite.close();
		return aStringAString.toString().replace("\r", "");
	}
}
